package iss.java;
import iss.java.list.MyList;
import iss.java.list.Node;
/**
 * Created by wenke on 2016/9/16.
 */
public class Main_A {
    public static final Object obj=new Object();

    public static void main(String[] args) throws InterruptedException{//入口
        // TODO: Implement a multithreaded test case against requirement A
        MyList list=new MyList();
        list.insert(list.getHead(), 50);         //插入节点并赋值
        Node n = list.getHead().getNext();

        Thread ta = new Thread() {              //线程1
            @Override
            public void run() {
                for (int i = 0; i < 100000; ++i) {
                    synchronized(obj){
                        n.setData( n.getData() +10);
                    }
            }//end for
            }//end run
        };//end thread ta
        Thread tb = new Thread() {            //线程2
            @Override
            public void run() {
                for (int i = 0; i < 100000; ++i) {
                    synchronized(obj) {
                        n.setData(n.getData() - 10);
                    }//end synchronized
                }//end for
            }//end run
        };//end thread tb
        ta.start();
        tb.start();
        // Ten seconds are enough for the two threads to finish
        Thread.sleep(1000);
        System.out.printf("Final result of x = %d\n", n.getData());
    }//end main
}//end Main_A



